package com.common.mybatis.dict.mapper;

import com.common.mybatis.dict.domain.EnumDict;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

public interface EnumDictMapper {

    @InsertProvider(method = "insertSql", type = EnumDictMapperProvider.class)
    int insert(EnumDict enumDict);

    @UpdateProvider(method = "updateSql", type = EnumDictMapperProvider.class)
    int update(EnumDict enumDict);

    @Select("select * from enum_dict where class_name=#{className} and enum_index=#{index}")
    EnumDict queryEnumDict(@Param("className")String className, @Param("index")int index);

    @Select("select * from enum_dict where class_name = #{className}")
    List<EnumDict> queryListByClassName(String className);

    @Select("select * from enum_dict")
    List<EnumDict> queryAllList();

    class EnumDictMapperProvider{
        public String insertSql(EnumDict enumDict){
            SQL sql = new SQL();
            sql.INSERT_INTO("enum_dict");
            sql.VALUES("class_name","#{className}");
            sql.VALUES("description","#{description}");
            sql.VALUES("enum_index","#{enumIndex}");
            sql.VALUES("enum_name","#{enumName}");
            sql.VALUES("enum_description","#{enumDescription}");
            sql.VALUES("futures_json","#{futuresJson}");
            sql.VALUES("create_time","#{createTime}");
            sql.VALUES("update_time","#{updateTime}");
            return sql.toString();
        }
        public String updateSql(EnumDict enumDict){
            SQL sql = new SQL();
            sql.UPDATE("enum_dict");
            sql.SET("class_name = #{className}");
            sql.SET("description = #{description}");
            sql.SET("enum_index = #{enumIndex}");
            sql.SET("enum_name = #{enumName}");
            sql.SET("enum_description = #{enumDescription}");
            sql.SET("futures_json = #{futuresJson}");
            sql.SET("update_time = #{updateTime}");
            sql.WHERE("id = #{id}");
            return sql.toString();
        }
    }
}
